package com.example.cofeed.activity;

import android.content.Intent;
import android.support.v7.widget.DividerItemDecoration;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;

import com.example.cofeed.R;
import com.example.cofeed.adapter.SupplyDemandAdapter;
import com.example.cofeed.api.ApiService;
import com.example.cofeed.base.BaseListActivity;
import com.example.cofeed.callback.DialogCallback;
import com.example.cofeed.callback.LzyResponse;
import com.example.cofeed.constant.IntentKeyConstant;
import com.example.cofeed.entity.Supply;
import com.lzy.okgo.model.Response;
import com.umeng.analytics.MobclickAgent;

import java.util.List;

public class SupplyDemandActivity extends BaseListActivity<Supply> {

    private SupplyDemandAdapter mSupplyDemandAdapter;
    private int mType;
    private int mId;

    @Override
    protected void bindRecyclerView(RecyclerView recLv) {
        setMainTitle("供求列表");

        mType = getIntent().getIntExtra(IntentKeyConstant.TYPE, 0);
        mId = getIntent().getIntExtra(IntentKeyConstant.ID, 0);

        recLv.setLayoutManager(new LinearLayoutManager(this));
        recLv.addItemDecoration(new DividerItemDecoration(this, DividerItemDecoration.VERTICAL));
        mSupplyDemandAdapter = new SupplyDemandAdapter();
        recLv.setAdapter(mSupplyDemandAdapter);
        mSupplyDemandAdapter.setOnItemClickListener((adapter, view, position) -> {
            Supply supply = (Supply) adapter.getData().get(position);
            Intent intent = new Intent(mContext, WebViewActivity.class);
            intent.putExtra(IntentKeyConstant.ID, supply.id);
            intent.putExtra(IntentKeyConstant.URL, supply.urlpath);
            intent.putExtra(IntentKeyConstant.TYPE, -1);
            startActivity(intent);
        });

        mSupplyDemandAdapter.setOnItemChildClickListener((adapter, view, position) -> {
            Supply supply = (Supply) adapter.getData().get(position);
            if (view.getId() == R.id.ll_change){
                Intent intent = new Intent(mContext, DemandIssueActivity.class);
                intent.putExtra(IntentKeyConstant.DEMAND_ID, supply.id);
                startActivity(intent);
            }
            else if (view.getId() == R.id.ll_refresh){
                ApiService.supply_refresh(this, supply.id, new DialogCallback<LzyResponse<Object>>(this) {
                    @Override
                    public void onSuccess(Response<LzyResponse<Object>> response) {
                        autoRefresh();
                    }
                });

            }

        });

    }

    @Override
    protected void loadData(int page, int pageCount) {

        ApiService.supply_get_user_list(this, page, new DialogCallback<LzyResponse<List<Supply>>>() {
            @Override
            public void onSuccess(Response<LzyResponse<List<Supply>>> response) {
                onGetDataSuc(response.body().data);
            }
        });

    }

    @Override
    protected List<Supply> getData() {
        return mSupplyDemandAdapter.getData();
    }

    @Override
    protected void bindData(boolean isRefresh, List<Supply> list) {
        if (isRefresh){
            mSupplyDemandAdapter.setNewData(list);
        }else {
            mSupplyDemandAdapter.addData(list);
        }
    }

    @Override
    public void onResume() {
        super.onResume();
        MobclickAgent.onResume(this);
    }

    @Override
    public void onPause() {
        super.onPause();
        MobclickAgent.onPause(this);
    }

}
